package top.chukongxiang.project.base.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.chukongxiang.project.base.model.vo.KeywordsPageVo;

/**
 * @author 楚孔响
 * @version 1.0
 * @date 2022/2/24 20:07
 */
public class KeywordsPageService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {

    /**
     * 关键词搜索
     * @param keywordsPage
     * @param fields
     * @return
     */
    IPage<T> keywordsPageByField(KeywordsPageVo<T> keywordsPage, SFunction<T, Object>... fields) {
        if (keywordsPage == null) {
            return new Page<>();
        }
        LambdaQueryWrapper<T> wrapper = Wrappers.lambdaQuery();
        if (fields != null && fields.length > 0 && !StringUtils.isEmpty(keywordsPage.getKeywords())) {
            for (int i = 0; i < fields.length; i++) {
                if (i != 0) {
                    wrapper.or();
                }
                wrapper.like(fields[i], keywordsPage.getKeywords());
            }
        }
        return page(keywordsPage, wrapper);
    }
}
